package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.dao.jdbc.VacationTwoJdbc;
import com.wuseguang.springcloud.service.VacationTwoService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.PaginationUtil;
import com.wuseguang.springcloud.util.PagingTool;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class VacationTwoServiceImpl implements VacationTwoService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private VacationTwoJdbc vacationJdbc;

    //推送接口
    @Autowired
    private ClassCardService classCardService;


    /**
     * @描述 组合查询学生请假信息情况 2.0 小程序
     * @创建人 zm
     * @创建时间 2020/3/26
     */
    @Override
    public Result queryStudentVacationAll2(Integer indexNo, Integer teacherId, Integer schoolId, Integer state, Integer rows, Integer classId) {
        //0 待审核 (1第一审核通过 2通过) 3第一审核人拒绝 4销假中 5已销假 6第二审核人未通过
        int approver = -1;//第一审批人
        String sql = " SELECT GROUP_CONCAT(c.id) string FROM  school_vacation s,teacher_role r,classes c WHERE s.one_roles_id=r.role_id  AND r.type=3 " +
                "  AND s.school_id =r.school_id AND r.school_id=? AND r.pid=c.id AND c.graduation=0 AND r.teacher_id=? ";

        if (!StringUtils.isEmpty(classId)) {
            sql += " AND c.id=" + classId;
        }

        String s = vacationJdbc.queryString(sql, schoolId, teacherId);//存在时为第一审批人

        if (StringUtils.isEmpty(s)) {//不存在时 查第二审批人
            approver = 2;//第二审批人
            sql = " SELECT GROUP_CONCAT(c.id) string FROM  school_vacation s,teacher_role r,classes c WHERE s.two_roles_id=r.role_id  AND r.type=2 " +
                    "  AND s.school_id =r.school_id AND r.school_id=?  AND r.pid=c.grade_id AND c.graduation=0 AND r.teacher_id=? ";
            if (!StringUtils.isEmpty(classId)) {
                sql += " AND c.id=" + classId;
            }
            s = vacationJdbc.queryString(sql, schoolId, teacherId);//存在时为第二审批人
        } else {
            approver = 0;//第一审批人 没有第二审批人
            sql = " SELECT two_roles_id string FROM school_vacation WHERE school_id=?";
            if (!StringUtils.isEmpty(vacationJdbc.queryString(sql, schoolId))) {//有第二审批人
                approver = 1;
            }
        }

        List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
        if (!StringUtils.isEmpty(s)) {
            sql = "SELECT s.student_name name ,CONCAT('提交了',IF(type=0,'病假','事假')) title,s.head_portrait,v.id,v.student_id,v.type,v.school_id," +
                    "DATE_FORMAT(v.stime,'%Y-%m-%d %H:%i') stime,DATE_FORMAT(v.etime,'%Y-%m-%d %H:%i') etime,v.state,v.content,v.teacher_id," +
                    "DATE_FORMAT(v.opertime ,'%Y-%m-%d %H:%i') opertime ,c.name classname," + approver + " approver FROM vacation v,student s,classes c WHERE c.graduation=0 " +
                    "AND s.id=v.student_id AND c.id=s.class_id AND v.school_id=? AND s.class_id in (" + s + ") ";

            String states = state + "";

            //0 待审核 (1第一审核通过 2通过) 3第一审核人拒绝 4销假中 5已销假 6第二审核人未通过

            if (state == 0 && approver == 2) {//查询 待审核  第二审核人查看 第一审核人通过的
                states = "1";
            }

            if (state == 1) {//查询 通过
                states = "2,4,5";  //2通过 4 销假中 5 已销假 均为通过状态
            }

            if (state == 1 && (approver == 0 || approver == 1)) {//查询 第一审批人通过
                states = "1,2,4,5,6";  //2通过 4 销假中 5 已销假 均为通过状态  6第二审核人未通过(说明第一审核人已通过)
            }

            if (state == 2 && approver == 2) {//查询 第二审批人审核未通过
                states = "6";
            }

            if (state == 2 && (approver == 0 || approver == 1)) {//查询 第一审批人审核未通过
                states = "3";
            }

            sql += " AND v.state in (" + states + ") order by opertime desc";

            maps = vacationJdbc.queryListMap(sql, schoolId);
        }

        //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(maps);
        return new Result(new PaginationUtil<>(maps.size(), rows, tool.getList(), indexNo));
    }


    /**
     * @描述 修改请假状态
     * @创建人 zm
     * @创建时间 2020/3/26
     */
    @Override//0 待审核 (1第一审核通过 2通过) 3第一审核人拒绝 4销假中 5已销假 6第二审核人未通过
    public Result updateStudentVacationState(Integer id, Integer state, Integer teacherId) {
        String sql = "UPDATE vacation SET state=?,teacher_id=? WHERE id=? ";
        int i = vacationJdbc.updateByParameter(sql, state, teacherId, id);
        if (i > 0) {
            sql = "SELECT class_id ints FROM student t,vacation v WHERE v.id=? AND t.id=v.student_id ";
            Integer integer = vacationJdbc.queryInteger(sql, id);
            sql = "UPDATE bancard SET noticeflag=1 WHERE classroomid IN (SELECT id FROM  classroom WHERE classid=? )";
            vacationJdbc.updateByParameter(sql, integer);
            classCardService.classesAndCards(integer);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }
}
